package bid.yanhj.ssm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import bid.yanhj.common.service.FormatService;
import bid.yanhj.common.service.ResultService;
import bid.yanhj.common.util.BeanUtil;
import bid.yanhj.ssm.po.Items;
import bid.yanhj.ssm.po.ItemsCustom;
import bid.yanhj.ssm.po.ItemsQueryVo;
import bid.yanhj.ssm.service.ItemsService;

@Controller
public class ItemsJsonController {

	private static final Logger LOG = LoggerFactory.getLogger(ItemsJsonController.class);

	@Autowired
	private ItemsService itemsService;

	/**
	 * 查询所有商品信息
	 * @param itemsQueryVo
	 * @return 商品信息
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryItemsList")
	@ResponseBody
	public Object queryItemsList(@RequestBody ItemsQueryVo itemsQueryVo) throws Exception {
		LOG.debug("【接受前端报文");
		List<ItemsCustom> itemsList = itemsService.findItemsList(itemsQueryVo);
		return ResultService.success(itemsList);
	}

	/**
	 * 以ID查询商品信息
	 * @param items
	 * @return 商品信息
	 */
	@RequestMapping(value = "/findItemsById")
	@ResponseBody
	public Map<String, Object>   findItemsById(@RequestBody Items items) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		ItemsCustom itemsCustom = null;
		try {
			itemsCustom = itemsService.findItemsById(items.getId());
			if (itemsCustom !=null) {
				resultMap = BeanUtil.transBean2Map(itemsCustom);
				resultMap.put("msg", "成功");
                resultMap.put("status", 0);
			} else {
				LOG.error(FormatService.logFormat("根据id查询商品为空。"));
				resultMap.put("msg", "订单查询失败");
                resultMap.put("status", 1);
			}
		} catch (Exception e) {
//			LOG.error(FormatService.logFormat(e.getMessage()));
			LOG.error(FormatService.logFormat("findOrderById error。"), e);
		      resultMap.put("msg", "订单查询失败");
	          resultMap.put("status", 3);
		}
		return resultMap;
	}

	/**
	 * 新增更新商品
	 * @param items
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/insertAndUpdataItem")
	public Map<String, Object> insertAndUpdataItem(@RequestBody ItemsCustom itemsCustom) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        
        try {
			itemsService.updateItems(itemsCustom.getId(), itemsCustom);
            resultMap.put("msg", "操作成功");
            resultMap.put("status", 0);
		} catch (Exception e) {
            LOG.error(FormatService.logFormat("insertAndUpdataItem error."), e);
            resultMap.put("msg", "操作失败");
            resultMap.put("status", 2);
            return resultMap;
		}
        
		return resultMap;
	}
}
